<template>
  <!-- 弹框组件 -->
  <el-dialog
    :visible="dialogVisible"
    width="680px"
    :title="`${ editid === 0 ? '添加计费规则' : '编辑计费规则' }`"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <!-- 表单接口 -->
    <div class="form-container">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
        <el-form-item label="计费规则编号" prop="ruleNumber">
          <el-input v-model="addForm.ruleNumber" />
        </el-form-item>
        <el-form-item label="计费规则名称" prop="ruleName">
          <el-input v-model="addForm.ruleName" />
        </el-form-item>
        <el-form-item label="计费方式" prop="chargeType">
          <el-radio-group v-model="addForm.chargeType" size="small" @change="groupChange">
            <el-radio label="duration" border>时长收费</el-radio>
            <el-radio label="turn" border>按次收费</el-radio>
            <el-radio label="partition" border>分段收费</el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="flex-container" style="display: flex; justify-content: space-between;">
          <el-form-item label="免费时长">
            <el-input v-model="addForm.freeDuration" style="width:150px" />
          </el-form-item>
          <el-form-item label="收费上限">
            <el-input v-model="addForm.chargeCeiling" style="width:150px" />
          </el-form-item>
        </div>
        <!-- el-form-item里面可以继续嵌套el-form-item -->
        <!--  -->
        <el-form-item label="计费规则" required>
          <!-- 按时长收费区域 -->
          <div v-if="addForm.chargeType === 'duration'" class="duration" style="display: flex;">
            每
            <el-form-item prop="durationTime">
              <el-input v-model="addForm.durationTime" class="input-box" style="display: flex;" />
            </el-form-item>
            小时
            <el-form-item prop="durationPrice">
              <el-input v-model="addForm.durationPrice" class="input-box" style="width:150px" />
            </el-form-item>
            元
          </div>
          <!-- 按次收费区域 -->
          <div v-if="addForm.chargeType === 'turn'" class="turn" style="display:flex;">
            每次
            <el-form-item prop="turnPrice">
              <el-input v-model="addForm.turnPrice" class="input-box" style="width:150px" />
            </el-form-item>
            元
          </div>
          <!-- 按分段收费区域 -->
          <div v-if="addForm.chargeType === 'partition'" class="partition">
            <div class="item" style="display: flex;">
              <el-form-item prop="partitionFrameTime">
                <el-input v-model="addForm.partitionFrameTime" class="input-box" style="width:120px;" />
              </el-form-item>
              小时内,每小时收费
              <el-form-item prop="partitionFramePrice">
                <el-input v-model="addForm.partitionFramePrice" class="input-box" style="width:120px;" />
              </el-form-item>
              元
            </div>
            <div class="item" style="display: flex; margin: 30px auto 0">
              <el-form-item prop="partitionIncreaseTime">
                <el-input v-model="addForm.partitionIncreaseTime" class="input-box" style="width:120px;" />
              </el-form-item>
              小时超出后, 增加
              <el-form-item prop="partitionIncreasePrice">
                <el-input v-model="addForm.partitionIncreasePrice" class="input-box" style="width:120px;" />
              </el-form-item>
              元
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button size="mini" type="primary" @click="addConfirm">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addFeeRuleAPI, editRuleListAPI, showRuleListAPI } from '@/api/carRule'
export default {
  name: 'AddRules',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      addForm: {
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱
      },
      // 计费规则表单对象
      addFormRules: {
        ruleNumber: [
          { required: true, message: '请输入规则编号', trigger: 'blur' }
        ],
        ruleName: [
          { required: true, message: '请输入规则名称', trigger: 'blur' }
        ],
        chargeType: [
          { required: true, message: '请选择收费类型', trigger: 'blur' }
        ],
        durationTime: [
          { required: true, message: '请输入时间', trigger: 'blur' }
        ],
        durationPrice: [
          { required: true, message: '请输入金额', trigger: 'blur' }
        ],
        turnPrice: [
          { required: true, message: '请输入每次的价格', trigger: 'blur' }
        ],
        partitionFrameTime: [
          { required: true, message: '请输入段内时长', trigger: 'blur' }
        ],
        partitionFramePrice: [
          { required: true, message: '请输入段内费用', trigger: 'blur' }
        ],
        partitionIncreaseTime: [
          { required: true, message: '请输入超出时间', trigger: 'blur' }
        ],
        partitionIncreasePrice: [
          { required: true, message: '请输入超出金额', trigger: 'blur' }
        ]
      },
      editid: 0

    }
  },
  computed: {
  },
  // 2.添加watch事件，进行监听，监听chargeType的值是否发生改变，改变就清空表单校验结果
  watch: {
    'addForm.chargeType'(val) {
      // console.log(val)
      // 方案一：每当切换到对应的计费方式，就清空对应的错误提示和校验结果
      /*  if (val === 'turn') {
        this.$refs.addForm.clearValidate(['durationTime', 'durationPrice', 'partitionIncreaseTime', 'partitionIncreasePrice', 'partitionFrameTime', 'partitionFramePrice'])
      }
      if (val === 'partition') {
        this.$refs.addForm.clearValidate(['durationTime', 'durationPrice', 'turnPrice'])
      }
      if (val === 'duration') {
        this.$refs.addForm.clearValidate(['turnPrice', 'partitionIncreaseTime', 'partitionIncreasePrice', 'partitionFrameTime', 'partitionFramePrice'])
      } */
      // 方案二：当切换计费方式时，清空所有的错误提示和校验结果
      this.$refs.addForm.clearValidate(['turnPrice', 'durationTime', 'durationPrice', 'partitionIncreaseTime', 'partitionIncreasePrice', 'partitionFrameTime', 'partitionFramePrice'])
    }
  },

  methods: {
    // 关闭弹窗
    closeDialog() {
      // 子组件不能修改父组件中传过来的数据（谁的数据谁负责）
      // 解决方法：1.子传父
      // 2.this.$parent.dialogVisible = false
      // 3.  .sync修饰符 子组件和父组件中的数据进行双向绑定[父组件加上.sync修饰符之后表示放权给子组件修改props数据]
      // 固定语法：this.$emit('update：props传过来的值'，要修改的值)
      this.$emit('update:dialogVisible', false)
      // 清空绑定了prop的数据
      this.$refs.addForm.resetFields()
      // 清空免费时长和收费上限字段(未绑定prop)
      this.addForm.freeDuration = null
      this.addForm.chargeCeiling = null
      // 关闭表单清除id
      this.editid = 0
      // console.log(this.editid)

      // 关闭表单修改计费规则为duration
    },
    // 切换计费规则，清空校验结果(此处未写，使用了watch方法)
    groupChange(prop) {
      console.log(prop)
    },
    // 添加表单的提交方法
    addConfirm() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        if (this.editid === 0) { // 添加新的计费规则
          await addFeeRuleAPI(this.addForm)
          this.$message.success('添加成功!')
        }
        if (this.editid !== 0) { // 编辑计费规则
          await editRuleListAPI(this.addForm)
          this.$message.success('编辑成功!')
          // this.getRuleList()
        }
        // 添加或者编辑成功后刷新页面（此处拆分子组件调用父组件中的方法）
        // 1.this.$parent  用来获取上层组件的实例
        //   this.$parent.getRuleList()
        // 2.this.$emit('getRuleList')
        // 子组件中通过$emit触发一个事件，父组件监听该事件，实现调用getRuleList方法
        this.$parent.$emit('GetRuleList')
        this.$emit('GetRuleList')
        // 添加成功，关闭弹窗
        this.closeDialog()
      })
    },
    async showRuleList(id) {
      // console.log('执行了')
      const res = await showRuleListAPI(id)
      this.addForm = res.data
      this.editid = id
      console.log(this.addForm)
    }
  }
}
</script>

